package cn.tedu.dimanage.user.webapi.controller;

import cn.tedu.dimanage.common.web.JsonResult;
import cn.tedu.dimanage.pojo.dto.UserAddNewDTO;
import cn.tedu.dimanage.pojo.dto.UserLoginDTO;
import cn.tedu.dimanage.pojo.dto.UserUpdateDTO;
import cn.tedu.dimanage.pojo.vo.UserDetailVO;
import cn.tedu.dimanage.pojo.vo.UserSimpleVO;
import cn.tedu.dimanage.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Classname: UserController
 * @Author: bromide
 * @CreateTime: 2022/6/10--13:07
 * @Version: V1.0
 * @Description:
 */
@RestController
@RequestMapping(value = "/user" , produces = "application/json;charset=utf-8")
public class UserController {

    @Autowired
    private IUserService userService;


    /*
        http://localhost:8080/user/add-new?username=test001&password=123456&
        name=litao&idNumber=429004199801154038&age=25&phone=13164644038&
        email=bromide1988@163.com
     */
    @RequestMapping("/add-new")
    public JsonResult<Void> addNew(@Validated UserAddNewDTO userAddNewDTO){
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    @RequestMapping("/parse/jwt")
    public JsonResult<UserSimpleVO> parseJwt(String jwt){
        UserSimpleVO userSimpleVO = userService.parseJwt(jwt);
        return JsonResult.ok(userSimpleVO);
    }

    @RequestMapping("/login")
    public JsonResult<String> login(@Validated UserLoginDTO userLoginDTO, HttpServletRequest request){
        // todo 获取用户登陆的ip
        String ip = request.getRemoteAddr();
        userLoginDTO.setLastLoginIp(ip);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    @RequestMapping("/list/details")
    public JsonResult<List<UserDetailVO>> listDetailsUser(Long roleId){
        List<UserDetailVO> list = userService.listDetailsUser(roleId);
        return JsonResult.ok(list);
    }
    @RequestMapping("/details")
    public JsonResult<UserDetailVO> detailsUser(String username){
        UserDetailVO userDetailVO = userService.detailsUser(username);
        return JsonResult.ok(userDetailVO);
    }

    @RequestMapping("/deleteUserById")
    public JsonResult<Void> deleteUserById(Long id){
        userService.deleteUserById(id);
        return JsonResult.ok();
    }

    @RequestMapping("/updateUser")
    public JsonResult<Void> updateUser(UserUpdateDTO userUpdateDTO){
        userService.updateUser(userUpdateDTO);
        return JsonResult.ok();
    }

}
